import os
from typing import Dict

import yaml


class YamlUtil:
    API_CASE_PATH = "/pytest_cases/"

    def read_yaml_file(self):
        """
        读取文件,读取yml文件里的参数,os.getcwd(): 返回当前进程的工作目录
        :return: dict对象
        """
        with open(os.getcwd() + "/extract.yml", mode="r", encoding="UTF-8") as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value

    def write_yaml_file(self, data):
        """
        写入文件,将参数写入yml文件里
        :param clear_yml_data:
        :param data:
        :return: yml file
        """
        with open(os.getcwd() + "/extract.yml", mode="a", encoding="utf-8") as f:
            yaml.dump(data=data, stream=f, allow_unicode=True)

    def clear_yaml_file(self):
        """
        清楚yml文件数据(解决：多次写入文件数据内容重复追加)
        """
        with open(os.getcwd() + "/extract.yml", mode="w", encoding="utf-8") as f:
            f.truncate()

    def read_yaml_case(self, yaml_name):
        """
        读取测试用例的数据文件
        :return: dict对象
        """
        with open(os.getcwd() + YamlUtil.API_CASE_PATH + yaml_name, mode="r", encoding="UTF-8") as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value
